﻿// -----------------------------------------------------------------------
// <copyright file="ExcelHelper.cs" company="None">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ODataTestFrameworkCore.shared
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Interop.Excel;
    using System.Diagnostics.Contracts;
    using System.IO;
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ExcelHelper :IDisposable
    {
        public Application app;
       public  Sheets wsSheet;
       public Worksheet CurSheet;
       public  Workbook wbWorkbook;
       string outputFileName = string.Empty;
       public ExcelHelper(string outputFileName)
        {
           
            app = new Microsoft.Office.Interop.Excel.Application();
           
             wbWorkbook = app.Workbooks.Add(Type.Missing);
             wsSheet = wbWorkbook.Worksheets;
             this.outputFileName = outputFileName;

           
        }

        public Range SetCell(string value,int row,int column, int WorkSheetIndex)
        {
            
            Contract.Requires(row > 0, "Row must be greater than zero");
            Contract.Requires(column > 0, "Column must be greater than zero");

            CurSheet = (Microsoft.Office.Interop.Excel.Worksheet)wsSheet[WorkSheetIndex];
            Range thisCell = (Microsoft.Office.Interop.Excel.Range)CurSheet.Cells[row, column];
            thisCell.Value = value;
            return thisCell;
        }

        public void SaveAS(string outputFileName)
        {
            string filename = Path.Combine(outputFileName);
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

           
        }
        public void Dispose()
        {
            if (app != null && wbWorkbook!=null)
            {
                this.wbWorkbook.SaveAs(outputFileName);
                wbWorkbook.Close();
            }

            app.Quit();
            
            
        }

        
    }
}
